package com.yungu.swift.common.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.yungu.swift.assets.driver.service.DriverService;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.config.DriverConfig;
import com.yungu.swift.common.model.param.AdminConfigParam;
import com.yungu.swift.common.service.CommonConfigService;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.utils.JsonUtils;
import com.yungu.swift.utils.MapUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.Properties;

@Slf4j
@Service
@Configuration
public class CommonConfigServiceImpl implements CommonConfigService {

    @Value("${nacos.host:127.0.0.1:8848}")
    private String host;
    @Value("${nacos.namespace:}")
    private String namespace;
    private String GROUP = "DEFAULT_GROUP";

    private ConfigService configService;

    @Reference
    private DriverService driverService;

    @PostConstruct
    public void init() {
        Properties properties = new Properties();
        properties.put("serverAddr", host);
        properties.put("namespace", namespace);
        try {
            configService = NacosFactory.createConfigService(properties);
        } catch (NacosException e) {
            log.error("init configService error.", e);
        }

    }

    @Override
    public ResponseData<Boolean> addOrUpdateConfig(AdminConfigParam adminConfigParam) {
        for (AdminConfigParam.CommonConfigAddParam addParam : adminConfigParam.getConfigAddParamList()) {
            try {
                //当顺路模式跟不接火车站开关被关闭时 需要将已开启顺路模式跟不接火车站订单的司机关闭
                if (addParam.getConfigKey().equals("DRIVER_CONFIG")) {
                    DriverConfig driverConfig = JsonUtils.readValue(addParam.getConfigValue(), DriverConfig.class);
                    handleSameWayModel(driverConfig);
                    handleStationModel(driverConfig);
                }
                log.info("配置保存参数:{}", JsonUtils.toJSONString(adminConfigParam));
                configService.publishConfig(addParam.getConfigKey(), GROUP, addParam.getConfigValue());
            } catch (NacosException e) {
                log.error("configService publishConfig error.", e);
            } catch (Exception e) {
                log.error("解析配置json信息失败", e);
            }
        }
        return ResponseData.buildSuccessResponse(Boolean.TRUE);
    }

    /**
     * 当触发顺路模式关闭时 将已开启顺路模式的司机关闭
     *
     * @param driverConfig
     */
    private void handleSameWayModel(DriverConfig driverConfig) {
        StringBuilder closeBusinessType = new StringBuilder();
        DriverConfig.OrderBean.RemindTypeBean remindTypeBean = driverConfig.getOrder().getRemindType();
        if (!remindTypeBean.getTaxi().isSameWayModel()) {
            closeBusinessType.append(",").append(CommonConstant.BUSINESS_TYPE_TAXI);
        }
        if (!remindTypeBean.getSpecial().isSameWayModel()) {
            closeBusinessType.append(",").append(CommonConstant.BUSINESS_TYPE_SPEC);
        }
        if (!remindTypeBean.getJoin().isSameWayModel()) {
            closeBusinessType.append(",").append(CommonConstant.BUSINESS_TYPE_POOL);
        }
        if (!remindTypeBean.getExpress().isSameWayModel()) {
            closeBusinessType.append(",").append(CommonConstant.BUSINESS_TYPE_EXPRESS);
        }
        if (closeBusinessType.length() > 0) {
            log.info("---关闭{}司机顺路模式---", closeBusinessType);
            driverService.closeDriverSameWayModel(closeBusinessType.toString());
        }
    }

    /**
     * 当触发不接火车站单关闭时 将已开启不接火车站模式的司机关闭
     *
     * @param driverConfig
     */
    private void handleStationModel(DriverConfig driverConfig) {
        StringBuilder closeBusinessType = new StringBuilder();
        DriverConfig.OrderBean.RemindTypeBean remindTypeBean = driverConfig.getOrder().getRemindType();
        if (!remindTypeBean.getTaxi().isRejectStationOrder()) {
            closeBusinessType.append(",").append(CommonConstant.BUSINESS_TYPE_TAXI);
        }
        if (!remindTypeBean.getSpecial().isRejectStationOrder()) {
            closeBusinessType.append(",").append(CommonConstant.BUSINESS_TYPE_SPEC);
        }
        if (!remindTypeBean.getJoin().isRejectStationOrder()) {
            closeBusinessType.append(",").append(CommonConstant.BUSINESS_TYPE_POOL);
        }
        if (!remindTypeBean.getExpress().isRejectStationOrder()) {
            closeBusinessType.append(",").append(CommonConstant.BUSINESS_TYPE_EXPRESS);
        }
        if (closeBusinessType.length() > 0) {
            log.info("---关闭{}司机不接火车站模式---", closeBusinessType);
            driverService.closeDriverStationModel(closeBusinessType.toString());
        }
    }
}
